
package com.scj.ho;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class MainActivityAdapter extends BaseAdapter {

    private Context context = null;

    private Item item = null;

    private ArrayList<HolidayBean> beans = null;

    public MainActivityAdapter(Context context, ArrayList<HolidayBean> beans) {

        this.context = context;
        this.beans = beans;

        if (this.beans != null) {
            // 用日期,part_id进行排序
            Collections.sort(this.beans, new Comparator<HolidayBean>() {

                public int compare(HolidayBean lhs, HolidayBean rhs) {
                    return -(lhs.getNumber() - rhs.getNumber());
                }

            });
        }

    }

    public int getCount() {
        return beans.size();
    }

    public Object getItem(int position) {
        return beans.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.hol_list_item, null);
            item = new Item();
            item.holiday_name = (TextView) convertView.findViewById(R.id.holiday_name);
            item.holiday_date = (TextView) convertView.findViewById(R.id.holiday_date);
            item.holiday_number = (TextView) convertView.findViewById(R.id.holiday_number);
            item.holiday_haiyou = (TextView) convertView.findViewById(R.id.holiday_haiyou);
            item.holiday_tian = (TextView) convertView.findViewById(R.id.holiday_tian);
            convertView.setTag(item);
        } else {
            item = (Item) convertView.getTag();
        }

        setValue(beans.get(position));
        return convertView;
    }

    private void setValue(HolidayBean bean) {
        item.holiday_name.setText(bean.getName());
        item.holiday_date.setText(bean.getDate());
        if (bean.getNumber() >= 0) {
            item.holiday_haiyou.setText(bean.getHaiyou());
            item.holiday_number.setText("" + bean.getNumber());
        } else {
            item.holiday_haiyou.setText(context.getString(R.string.over));
            item.holiday_number.setText("" + bean.getNumber()*-1);
        }
        
        item.holiday_tian.setText(bean.getTian());
    }

    class Item {

        TextView holiday_name;

        TextView holiday_date;

        TextView holiday_number;

        TextView holiday_haiyou;

        TextView holiday_tian;

    }

}
